package com.wmlt.dao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class BaseDao {
    public static Connection connection=null;
    public static DataSource ds=null;

    public static Connection getConnection() {
        //return DriverManager.getConnection(url, user, password);
        try{
            Context context=new InitialContext();
            ds=(DataSource) context.lookup("java:comp/env/jdbc/wmlt");
            connection=ds.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }
    /**
     * 关闭连接
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
